<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CloudimageType;
use App\Models\Cloudimage;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class CloudImageController extends Controller
{
    protected $model;
    public function __construct(Cloudimage $cloudimage)
    {
        $this->model = $cloudimage;
    }

    public function index(Request $request, Cloudimage $cloudimage)
    {
        $cloudimg = $this->model->filter($request->all())
            ->select('cloudimages.*', 'cloudimage_types.cloudimagesname as cate_name')
            ->leftJoin('cloudimage_types', 'cloudimages.category_id', '=', 'cloudimage_types.id')
            ->orderBy('cloudimages.created_at', 'desc')
            ->paginate(config('api.page.limit'));
            $categs = CloudimageType::all();
        $imagesName = $request->imagesName;
        return view('admins.cloudimage.index',compact('cloudimg','imagesName'));
    }
    
    public function lstcloud(Request $request, Cloudimage $cloudimage)
    {
        $cloudimg = $cloudimage->filter($request->all())->orderBy('id', 'desc')->paginate(config('api.page.limit'));
        $imagesName = $request->imagesName;
        return view('admins.cloudimage.lstcloud',compact('cloudimg','imagesName'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        $cloudimg = DB::table('cloudimages')->get();
        $categs = CloudimageType::all();
        return view('admins.cloudimage.add')->with('cloudimg', $cloudimg)->with('categs', $categs);
        //return response()->json(['flag' => $cloudimg, 'msg' => $msg]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     /*
      * 云图库
      */
    public function store(Request $request)
    {
        $categoryId = $request->categoryId;
        $imagesName = $request->imagesName;
        $base64Image = $request->base64Image;
        $imageUrl = is_https().$_SERVER['HTTP_HOST'];
        $time = date('Y-m-d H:i:s', time());
        $data = array(
            'category_id' => $categoryId,
            'images_name' => $imagesName,
            'base64_image' => $base64Image,
            'image_url' => $imageUrl,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result = 0;  
        if($data){
            $result = $this->model->insertGetId($data);
            $msg = '添加失败，请重试！';
            if ($result) {
                $msg = '添加成功！';
            }
        }
        return response()->json(['flag' => $result, 'msg' => $msg]);
    }
    public function uploadImage(Request $request)
    {
        $year_mouth = date("Ym", time());
        $path = 'storage/upload/images/' . $year_mouth . '/' . date("d", time()) . '/' . auth('admin')->user()->id;
        $upload_file = $request->file('base64Image');
        // 获取文件后缀
        $fileextension = $upload_file->getClientOriginalExtension();
        $filename = date("YmdHis", time()) . '_' . mt_rand(1, 999) . '.' . $fileextension;
        $upload_file->move($path, $filename);
        $uploadFileName = "/".$path . '/' . $filename;

        $upload_file->isValid();
        return response()->json(['images' => $uploadFileName]);
    }
}
